package com.guochenglang.www.service.impl;

import com.guochenglang.www.controller.CommentController;
import com.guochenglang.www.dao.ArticleDao;
import com.guochenglang.www.dao.CommentDao;
import com.guochenglang.www.dao.CommentMessageDao;
import com.guochenglang.www.dao.ReplyDao;
import com.guochenglang.www.pojo.Article;
import com.guochenglang.www.pojo.Comment;
import com.guochenglang.www.pojo.CommentMessage;
import com.guochenglang.www.pojo.Reply;
import com.guochenglang.www.service.CommentService;
import com.guochenglang.www.service.anotate.Affair;
import com.guochenglang.www.util.DangerWordBoolean;
import com.guochenglang.www.util.GetTable;
import com.guochenglang.www.util.MyDependency;
import com.guochenglang.www.util.lru.MyCapacity;

import java.sql.SQLException;
import java.util.ArrayList;

/**
 * 评论业务
 *
 * @author GuoChenglang
 */
public class CommentServiceImpl implements CommentService {
    //CommentController;
    private static final CommentController COMMENT_CONTROLLER = MyDependency.returnObject(CommentController.class);
    private static final CommentDao COMMENT_DAO = MyDependency.returnObject(CommentDao.class);
    private static final CommentMessageDao COMMENT_MESSAGE_DAO = MyDependency.returnObject(CommentMessageDao.class);
    private static final ArticleDao ARTICLE_DAO = MyDependency.returnObject(ArticleDao.class);
    private static final ReplyDao REPLY_DAO = MyDependency.returnObject(ReplyDao.class);

    /**
     * 进行评论
     *
     * @param comment 评论的信息
     * @return 主键
     */
    @Affair
    @Override
    public int comment(Comment comment) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        //敏感词判断
        String determine = comment.getComment();
        if (DangerWordBoolean.isDangerWord(determine)) {
            return -1;
        }
        Article condition = new Article();
        condition.setId(comment.getArticleId());
        //获得用户的id
        Integer userId = ARTICLE_DAO.articleSelect(condition).get(0).getCreaterId();
        //获得评论的主键
        int key = COMMENT_DAO.insertComment(comment);
        //提醒被评论的人
        COMMENT_MESSAGE_DAO.insertCommentMessage(new CommentMessage(null, 0, userId, key));
        return key;
    }

    /**
     * 删除评论
     *
     * @param id 评论的id
     * @return 删除行数
     */
    @Affair
    @Override
    public int deleteComment(Integer id) throws SQLException, InterruptedException, IllegalAccessException {
        //删除消息
        //封装条件
        CommentMessage messageCondition = new CommentMessage();
        Comment commentCondition = new Comment();
        Reply replyCondition = new Reply();
        messageCondition.setCommentId(id);
        commentCondition.setId(id);
        replyCondition.setCommentId(id);
        //删除消息
        COMMENT_MESSAGE_DAO.deleteCommentMessage(messageCondition);
        //删除评论
        COMMENT_DAO.deleteComment(commentCondition);
        //删除回复
        REPLY_DAO.deleteReply(replyCondition);
        return 0;
    }

    /**
     * 根据条件获取对应的评论
     *
     * @param conditions 获取条件
     * @return 具体评论
     */
    @Override
    public ArrayList<Comment> getComment(Comment conditions) throws SQLException, InterruptedException, IllegalAccessException, InstantiationException {
        return COMMENT_DAO.commentSelect(conditions);
    }
}
